// pages/information/pages/new-pub/index.js
import {
    input
} from '../../../../utils/tools'
import {http} from  '../../../../utils/apiutils'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: '',
        formats: {},
        readOnly: false,
        placeholder: '开始输入...',
        editorHeight: 300,
        keyboardHeight: 0,
        isIOS: false,
        content:'',
        content1:'',
        title:'',
        dataParams:{},
        is_first:true,
        showeditor:true
    },
    input: function (e) {
        input.bind(this)(e)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow () {
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    pub() {
        wx.showLoading()
        let data = Object.assign({},this.data.dataParams);
        data.avatar=getApp().globalData.userInfo.avatar,
        data.realName=getApp().globalData.userInfo.realName||getApp().globalData.userInfo.username,
        data.beFrom=2
        this.editorCtx.getContents({
            success(res){
                data.content=res.html
                http.newpub(data,res1=>{
                    wx.hideLoading()
                    wx.showToast({
                      title: res1.message+',返回列表',
                      icon:"none",
                      success(){
                         wx.nextTick(_=>{
                             wx.reLaunch({
                               url: '/pages/tabBar/information/index',
                             })
                         })
                      }
                    })
                },err=>{
                    wx.hideLoading()
                })
            }
        })
    },
    editinput(e) {
        this.setData({
            content1: e.detail.html,
            content: e.detail.text,
        })
    },
    readOnlyChange() {
        this.setData({
            readOnly: !this.data.readOnly
        })
    },
    onLoad(options) {
        if(options.item){
            this.setData({
                dataParams:JSON.parse(decodeURIComponent(options.item))
            })
        }
        const platform = wx.getSystemInfoSync().platform
        const isIOS = platform === 'ios'
        this.setData({
            isIOS
        })
        const that = this
        this.updatePosition(0)
        let keyboardHeight = 0
        wx.onKeyboardHeightChange(res => {
            if(res.height==0){
                this.editorCtx.blur()
            }
            if (res.height === keyboardHeight) return
            const duration = res.height > 0 ? res.duration * 1000 : 0
            keyboardHeight = res.height
            console.log(res)
            // that.updatePosition(keyboardHeight)
            // that.editorCtx.scrollIntoView()
            setTimeout(() => {
                wx.pageScrollTo({
                    scrollTop: keyboardHeight,
                    success() {
                        that.updatePosition(keyboardHeight)
                        that.editorCtx.scrollIntoView()
                    }
                })
            }, duration)
        })
    },
    updatePosition(keyboardHeight) {
        const toolbarHeight = 50
        const {
            windowHeight,
            platform
        } = wx.getSystemInfoSync()
        let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
        this.setData({
            editorHeight,
            keyboardHeight
        })
    },
    calNavigationBarAndStatusBar() {
        const systemInfo = wx.getSystemInfoSync()
        const {
            statusBarHeight,
            platform
        } = systemInfo
        const isIOS = platform === 'ios'
        const navigationBarHeight = isIOS ? 44 : 48
        return statusBarHeight + navigationBarHeight
    },
    onEditorReady() {
        const that = this
        wx.createSelectorQuery().select('#editor').context(function (res) {
            that.editorCtx = res.context
            if(that.data.dataParams.id){
                console.log(this.editorCtx)
                that.editorCtx.setContents({
                    html:that.data.dataParams.content,
                    success(){
                        that.editorCtx.blur()
                    }
                })
            }
        }).exec()
    },
    blur() {
        this.editorCtx.blur()
    },
    format(e) {
        let {
            name,
            value
        } = e.target.dataset
        if (!name) return
        this.editorCtx.format(name, value)
    },
    onStatusChange(e) {
        const formats = e.detail
        this.setData({
            formats
        })
    },
    insertDivider() {
        this.editorCtx.insertDivider({
            success: function () {
                console.log('insert divider success')
            }
        })
    },
    clear() {
        this.editorCtx.clear({
            success: function (res) {
                console.log("clear success")
            }
        })
    },
    removeFormat() {
        this.editorCtx.removeFormat()
    },
    insertDate() {
        const date = new Date()
        const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
        this.editorCtx.insertText({
            text: formatDate
        })
    },
    insertImage() {
        const that = this
        wx.chooseImage({
            count: 1,
            success: function (res) {
                let data={
                    url:res.tempFilePaths[0],
                    name:'file',
                    formData: {},
                }
                http.uploadFlie(data,res1=>{
                    that.editorCtx.insertImage({
                    src: res1.data,
                    width: '100%',
                    success: function () {
                    }
                })
                },err=>{
                })
                
            }
        })
    },
    aa(){
        let _this = this;
        setTimeout(()=>{
            _this.editorCtx.blur()
        },0)
    },
    focus(){
        this.setData({
            showeditor:false
        })
    },
    focus1(){
        this.setData({
            showeditor:true
        })
    },
})